﻿using DB2WORD.Models;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DB2WORD
{
    public partial class FrmInput : Form
    {
        public FrmInput()
        {
            InitializeComponent();
            this.Load += FrmInput_Load;
        }

        private Version Version { get; set; }


        private void SetTooltip()
        {
            this.toolTip1.SetToolTip(this.btnMajor, "主版本号迭代到" + (this.Version.Major + 1));
            this.toolTip1.SetToolTip(this.btnMinor, "次版本号迭代到" + (this.Version.Minor + 1));
            this.toolTip1.SetToolTip(this.btnBuild, "内部版本号迭代到" + (this.Version.Build + 1));
            this.toolTip1.SetToolTip(this.btnRevision, "修订版本号迭代到" + (this.Version.Revision + 1));
        }
        private void FrmInput_Load(object? sender, EventArgs e)
        {
            this.Version = MainForm.Project.Version;
            this.txtProjectname.Text = MainForm.Project.Name;
            this.txtVersion.Text = this.Version.ToString();
            this.SetTooltip();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //if(string.IsNullOrWhiteSpace(this.textBox1.Text))
            //{
            //    MessageBox.Show("请填写文档名称。");
            //    this.textBox1.Focus();
            //    return;
            //}
            //MainForm.Project.Name = this.textBox1.Text.Trim();

            if(string.IsNullOrWhiteSpace(this.txtVersion.Text))
            {
                MessageBox.Show("请填写文档版本。");
                this.txtVersion.Focus();
                return;
            }

            Version version;
            if(!Version.TryParse(this.txtVersion.Text.Trim(), out version))
            {
                MessageBox.Show("请填写正确的版本号。");
                this.txtVersion.Focus();
                return;
            }
            this.Version = version;

            MainForm.Project.ChangeLog = this.txtChange.Text.Trim();
            MainForm.Project.IsModified = true;
            if(MainForm.Project.Version != version)
            {
                MainForm.Project.Version = version;
                this.DialogResult = DialogResult.Yes;
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnMajor_Click(object sender, EventArgs e)
        {
            if(sender.Equals(this.btnMajor))
            {
                this.Version = new Version(this.Version.Major + 1, 0, 0, 0);
            }
            else if(sender.Equals(this.btnMinor))
            {
                this.Version = new Version(this.Version.Major, this.Version.Minor + 1, 0, 0);
            }
            else if(sender.Equals(this.btnBuild))
            {
                this.Version = new Version(this.Version.Major, this.Version.Minor, this.Version.Build + 1, 0);
            }
            else if(sender.Equals(this.btnRevision))
            {
                this.Version = new Version(this.Version.Major, this.Version.Minor, this.Version.Build, this.Version.Revision + 1);
            }
            this.txtVersion.Text = this.Version.ToString();
            this.SetTooltip();
        }
    }
}
